
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/8/9.
 */
public class NetWorkTest {


    private final String LOCALHOST = "127.0.0.1";

    private final String ANYHOST = "0.0.0.0";

    private final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");


    public NetWorkTest() {}

    /**
     * 遍历本地网卡，返回第一个合理的IP。
     *
     * @return 本地网卡IP
     */
    private InetAddress getLocalAddress0() {
        InetAddress localAddress = null;
        List<InetAddress> addressList=new ArrayList<InetAddress>();
        try {
            localAddress = InetAddress.getLocalHost();
            if (isValidAddress(localAddress)) {
                return localAddress;
            }
            if(localAddress!=null){
                addressList.add(localAddress);
            }
        } catch (Throwable e) {
        }
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            if (interfaces != null) {
                while (interfaces.hasMoreElements()) {
                    try {
                        NetworkInterface network = interfaces.nextElement();
                        Enumeration<InetAddress> addresses = network.getInetAddresses();
                        if (addresses != null) {
                            while (addresses.hasMoreElements()) {
                                try {
                                    InetAddress address = addresses.nextElement();
                                    if (isValidAddress(address)) {
                                        return address;
                                    }else if(address!=null){
                                        addressList.add(address);
                                    }
                                } catch (Throwable e) {
                                }
                            }
                        }
                    } catch (Throwable e) {
                    }
                }
            }
        } catch (Throwable e) {
        }
        //获取局域网ip
        if(addressList!=null&&addressList.size()>0){
            for (InetAddress inetAddress : addressList) {
                if(inetAddress.isSiteLocalAddress()){
                    return inetAddress;
                }
            }
        }
        return localAddress;
    }

    private boolean isValidAddress(InetAddress address) {
        //isLoopbackAddress:127.*,isSiteLocalAddress:局域网ip,isLinkLocalAddress:169.254.*,isMulticastAddress:224.0.0.0 ~ 239.255.255.255
        if (address == null || address.isLoopbackAddress() || address.isSiteLocalAddress() || address.isLinkLocalAddress()
                || address.isMulticastAddress() || isSpecialIp(address.getHostAddress())) {
            return false;
        }
        String name = address.getHostAddress();
        return (name != null && !ANYHOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name).matches());
    }

    private boolean isSpecialIp(String ip) {
        if (ip.contains(":")) return true;
        if (ip.equals("255.255.255.255")) return true;
        return false;
    }

    public static void main(String[] args){
        InetAddress address=new NetWorkTest().getLocalAddress0();
        if(address!=null)
        System.out.println("localhostAddress="+address.getHostAddress());
    }

}
